******************************************************************
** title: 		CHES Canada 			 						**
** authors:		dassonneville & koedam							**
** data:		CHES Canada survey 								**
** purpose: 	data analysis note on new data					**
** software:	Stata 18.0 										**	
******************************************************************

* --------------
* preamble
* --------------

	* uncomment installation line if the following package is not installed on your device yet
	*ssc install stripplot

	* set style for graphs
	grstyle init
	
	* set working directory and load data 
		/* Modify to your local working directory, in your working directory,
		create a folder 'data' for storing the data and also
		create a folder 'figures' where the figures that will be generated will
		be saved */
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use  "CHES_Canada 2023.dta", clear

* ------------------------------------------
* figure 1: describing the party space
* ------------------------------------------

	* working directory for saving graphs
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	
	* create position variable to ensure marker labels are not overlapping
	generate pos=3 if party_id==1
	replace pos=9 if party_id==2
	replace pos=9 if party_id==3
	replace pos=3 if party_id==4
	replace pos=12 if party_id==5
	replace pos=3 if party_id==6
	replace pos=3 if party_id==31
	replace pos=6 if party_id==32
	replace pos=6 if party_id==33
	replace pos=5 if party_id==34
	replace pos=9 if party_id==41
	replace pos=3 if party_id==42
	replace pos=9 if party_id==43
	replace pos=12 if party_id==44
	replace pos=6 if party_id==45

	*place federal, QC and OT parties in a two-dimensional space
	twoway (scatter galtan lrecon if party_id<=6, mlabel(party) msymbol(o) mcolor(green) mlabcolor(green%50) mlabsize(vsmall) mlabv(pos) xline(5, lpattern(dash)) yline(5, lpattern(dash)) xlabel(0(1)10) ylabel(0(1)10, nogrid) xtitle(Economic LR) ytitle(GAL-TAN) xsize(5) ysize(5) legend(region(lwidth(none)) row(1) size(small) order(1 "Federal" 2 "Ontario" 3 "Quebec") ) ) ( scatter galtan lrecon if party_id>=31 & party_id<=34, mlabel(party) msymbol(s) mcolor(red%50) mlabcolor(red) mlabsize(vsmall) mlabv(pos)) ( scatter galtan lrecon if party_id>=41 & party_id<=45, mlabel(party) mlabcolor(blue) mlabsize(vsmall) mlabv(pos) msymbol(d) mcolor(blue%50)) , saving(figure1.gph, replace)

	
* ------------------------------------------------------------------------------------------	
* figure 2: Canadian party polarization and dimensionality in comparative perspective	
* ------------------------------------------------------------------------------------------	

	* use separate file with polarization and dimensionality data
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use  "CHES_polarization.dta", clear
	
	* scatter plot of country polarization values
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	scatter galtan_v econ_v, title("") xtitle("Economic LR polarization") ytitle("GAL-TAN polarization") xlab(2(2)10) ylab(2(2)11, nogrid) mlabel(country) scheme(s2mono) graphregion(color(white)) || lfit econ_v econ_v, lpattern(solid) lwidth(thick) legend(off) saving(figure2a.gph, replace)

	* plot of effective dimensionality values
	graph dot ed_v, over(country, sort(1)) yscale(range(1 2)) exclude0 dots(mcolor(bg)) ytitle("Effective dimensionality")   graphregion(color(white)) marker(1, msymbol(O) mcolor(black)) saving(figure2b.gph, replace)
	
	
* ---------------------------------------------	
* appendix B: CHES and survey data
* ---------------------------------------------		

	* describe public opinion on income differences
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use "2021 Canadian Election Study v1.0.dta", clear
	recode pes21_gap  (6=.)

	sum pes21_gap if pes21_votechoice2021==1 // LPC
	sum pes21_gap if pes21_votechoice2021==2 // CP
	sum pes21_gap if pes21_votechoice2021==3 // NDP
	sum pes21_gap if pes21_votechoice2021==4 // BQ
	sum pes21_gap if pes21_votechoice2021==5 // GP
	sum pes21_gap if pes21_votechoice2021==6 // PPC    

	* merge CHES Canada with public opinion estimates
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use "CHES_Canada 2023.dta", clear
	merge 1:1 party using "ces-opinion.dta"
	drop _merge
	
	* scatter plots of party positions and public opinion
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	gen pos = 3
	replace pos = 9 if party_id == 4
	twoway lfit redistribution opinion_income || scatter redistribution opinion_income, mlabel(party) msymbol(O) mlabv(pos) ytitle(Party position on redistribution) xtitle(Electorate: efforts to reduce income gap) ylabel(0(1)10, nogrid) xlabel(1(1)5) legend(off) saving(figureb1.gph, replace)
		
	pwcorr opinion_income redistribution
	

* ---------------------------------------------------------------	
* appendix C: Comparing CHES to alternative party position data	
* ---------------------------------------------------------------	

	* merge CHES Canada with other party position estimates
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use "CHES_Canada 2023.dta", clear
	merge 1:1 party_id year using "Canada_v-party_gps.dta"
	drop _merge
		* Note: V-Party values taken from online explorer (2019)
		  * 'lrecon' recoded from 0-6 to 0-10 CHES scale
		  * 'immigr' recoded and reversed from 0-4 to 0-10 CHES scale
		* Note: GPS items are 'V4_Scale' 'V6_Scale' 'V10' 'V11' and 'V15'
	
	* correlation matrix
	corr lrecon immigrate galtan spendvtax ethnic_minorities vparty_lrecon_re vparty_immigr_re gps_lrecon gps_galtan gps_immigr gps_spending gps_ethnic

	* two-dimensional political space with CHES vs. GPS party positions
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	scatter galtan lrecon if party_id<6, title("") xtitle("Economic LR") ytitle("GAL-TAN") xlab(0(5)10) ylab(0(5)10, nogrid) yline(5, lpattern(dash)) xline(5, lpattern(dash)) mlabel(party) m(i) scheme(s2mono) graphregion(color(white)) || scatter gps_galtan gps_lrecon if party_id<6, mlabel(party_gps) mlabcolor(red) m(i) legend(off) saving(figurec1.gph, replace)


* ---------------------------------------------------
* appendix D: Expert agreement in party placements
* ---------------------------------------------------
	
	* table of mean and standard deviation by party
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use "CHES_Canada 2023.dta", clear	
	list party lrecon lrecon_sd galtan galtan_sd lrgen lrgen_sd if party_id<7
	
	* switch to expert-level data
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/data"
	use "CHES_Canada 2023_expert-level.dta", clear
	
	* dotplots of expert party placements by dimension
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	dotplot lrecon if party_id<7, over(party_id) mean center xtitle("Party") ytitle("Economic LR") xlabel(1 "CPC" 2 "LPC" 3 "NDP" 4 "BQ" 5 "GPC" 6 "PPC") mcolor(black) scheme(s1mono) graphregion(color(white)) saving(figured1a.gph, replace)

	dotplot galtan if party_id<7, over(party_id) mean center xtitle("Party") ytitle("GAL-TAN") xlabel(1 "CPC" 2 "LPC" 3 "NDP" 4 "BQ" 5 "GPC" 6 "PPC") mcolor(black) scheme(s1mono) graphregion(color(white)) saving(figured1b.gph, replace)

	dotplot lrgen if party_id<7, over(party_id) mean center xtitle("Party") ytitle("General LR") xlabel(1 "CPC" 2 "LPC" 3 "NDP" 4 "BQ" 5 "GPC" 6 "PPC") mcolor(black) scheme(s1mono) graphregion(color(white)) saving(figured1c.gph, replace)

	* boxplot of BQ placement by expert home region
	graph hbox lrecon galtan lrgen if party_id==4, over(QCexpert, sort(order) descending relabel(1 "Non-Quebec" 2 "Quebec")) ylabel(0(2)10) scheme(s2mono) graphregion(color(white)) legend(label(1 "Econ LR") label(2 "GAL-TAN") label(3 "Gen LR") rows(1)) saving(figured3.gph, replace)

	* generate standard deviations (SD) by party per issue
	collapse (sd) lrecon-members_vs_leadership, by(party_id)
	
	* produce plot with SDs
	gen party="CPC" if party_id==1
	replace party="LPC" if party_id==2
	replace party="NDP" if party_id==3
	replace party="BQ" if party_id==4
	replace party="GPC" if party_id==5
	replace party="PPC" if party_id==6
	
	cd "~/Dropbox/CHES Canada/article 1-data note/Harvard dataverse deposit/figures"
	stripplot immigrate multiculturalism redistribution environment spendvtax deregulation econ_interven civlib_laworder sociallifestyle religious_principles ethnic_minorities nationalism urban_rural protectionism regions indigenous public_health, by(party, note("")) msymbol(circle) mcolor(black) graphregion(color(white))  xtitle("") saving(figured2.gph, replace) 
